package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"net/http"
	"strconv"
	"strings"
)

type Patent struct {
	gorm.Model
	PatentNumber   string   `json:"patentNumber"`
	PatentName     string   `json:"patentName"`
	PatentCategory string   `json:"patentCategory"`
	PatentRanking  int      `json:"patentRanking"`
	PatentFile     []string `json:"patentFile"`
	UserId         uint     `json:"userid"`
	Year           int      `json:"year"`
}

func CreatePatent(c *gin.Context) {
	var patent model.Patent
	var data Patent
	userid, _ := c.Get("userid")
	_ = c.ShouldBindJSON(&data)
	//Year := model.GetYear()
	patent = model.Patent{
		PatentNumber:   data.PatentNumber,
		PatentName:     data.PatentName,
		PatentCategory: data.PatentCategory,
		PatentRanking:  data.PatentRanking,
		PatentFile:     strings.Join(data.PatentFile, ","),
		UserId:         userid.(uint),
		Year:           data.Year,
	}
	if err := model.CreatePatent(patent); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "添加失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "添加成功",
				"status": http.StatusOK,
			},
		})
	}
}
func GetPatent(c *gin.Context) {
	ptid, _ := strconv.Atoi(c.Param("id"))

	if err, repatent := model.GetPatent(uint(ptid)); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "添加失败",
			"status": http.StatusNotFound,
		})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": repatent,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}
func GetPatents(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	userid, _ := strconv.Atoi(c.Query("userid"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, patent, total := model.GetPatents(uint(userid), pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "添加失败",
			"status": http.StatusNotFound,
		})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"patentList": patent},
			"total": total,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}
func UpdatePatent(c *gin.Context) {
	var data Patent
	_ = c.ShouldBindJSON(&data)
	//Year := model.GetYear()
	patent := model.Patent{
		PatentNumber:   data.PatentNumber,
		PatentName:     data.PatentName,
		PatentCategory: data.PatentCategory,
		PatentRanking:  data.PatentRanking,
		PatentFile:     strings.Join(data.PatentFile, ","),
		Year:           data.Year,
	}
	patent.ID = data.ID

	if err := model.UpdatePatent(patent); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}
func DeletePatent(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.DeletePatent(id); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "删除成功",
				"status": http.StatusOK,
			},
		})
	}
}
func ChangePatentVisiblity(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.ChangePatentVisiblity(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}
func GetAllPatens(c *gin.Context) {
	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))
	if err, data, tt := model.GetAllPatens(userid.(uint), year); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"patentList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}
func GetPatentsDynamic(c *gin.Context) {
	var searchinfo request.PatentSearchInfo
	_ = c.ShouldBindQuery(&searchinfo)

	if err, list, total := service.GetPatentsDynamic(searchinfo); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.Fail(c)
	} else {
		response.OkWithData(response.PageResult{
			Total: total,
			List:  list,
		}, c)
	}
}
